package elitech.vietnam.weatherfashion.backgroundtask;

import java.io.IOException;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.Activity;
import android.os.AsyncTask;
import android.view.WindowManager;
import elitech.vietnam.weatherfashion.LoginActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;
import elitech.vietnam.weatherfashion.ultilities.ValueOption;

public class LoginTask extends AsyncTask<String, String, Boolean> {

	private LoginActivity activity;
	
	public static final String EXTRA_MESSAGE = "message"; 
	public static final String PROPERTY_REG_ID = "registration_id"; 
	String SENDER_ID = "793977106476";
	
	GoogleCloudMessaging mGcm;

	public LoginTask(LoginActivity activity) {
		this.activity = activity;
		mGcm = GoogleCloudMessaging.getInstance(WFApplication.getInstance());
	}

	@Override
	protected Boolean doInBackground(String... params) {
		String username = params[0];
		String password = params[1];

		Boolean response = false;
		PHPSoapServiceCaller appService = new PHPSoapServiceCaller();
		MemberDTO user = GlobalValue.getInstance(activity).readUserInfo();
		ValueOption option = GlobalValue.getInstance(activity).readOptionInfo();
		option.setUsername(username);
		option.setPassword("");

		try {
			user = appService.logIn(username, password);
			user.setGcmId(mGcm.register(SENDER_ID));
			appService.setGCMID(user.getId(), user.getGcmId());
		} catch (IOException e) {
			e.printStackTrace();
			user = null;
			publishProgress(activity
					.getString(R.string.cannot_connect_to_server));
			response = false;
		}
		if (user == null) {
			publishProgress(activity
					.getString(R.string.the_username_or_password_is_incorrect_));
			response = false;
		} else if (!user.getMemberArea().equals(Const.COUNTRY_CODE)) {
			user = null;
			publishProgress(activity
					.getString(R.string.the_username_or_password_is_incorrect_));
			response = false;
		}
		GlobalValue.getInstance(activity).writeUserInfo(user);
		if (user != null)
			try {
				Utils.getFriendList(activity);
				Utils.getStylerFriendData(activity);
				Utils.getSelfData(activity);
				option.setPassword(password);
				response = true;
			} catch (IOException e) {
				e.printStackTrace();
			}

		GlobalValue.getInstance(activity).writeOptionInfo(option);

		return response;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		activity.mProgressDialog.show();
	}

	@Override
	protected void onPostExecute(Boolean result) {
		activity.mProgressDialog.dismiss();
		if (result) {
			// hide keyboard
			try {
				activity.getWindow()
						.setSoftInputMode(
								WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
			} catch (Exception e) {
			}
			Utils.clearData();
			activity.setResult(Activity.RESULT_OK);
			WFApplication.getInstance().startGCMNotification();
			activity.finish();
			activity = null;
		}
	}

	@Override
	protected void onProgressUpdate(String... values) {
		super.onProgressUpdate(values);
		activity.setError(values[0]);
	}
}
